from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.expected_conditions import *
from selenium.webdriver.support.wait import WebDriverWait
from pages.film_edit_page import FilmEditPage
from pages.film_page import FilmPage
from pages.internal_page import InternalPage
from pages.login_page import LoginPage
from pages.film_edit_page import FilmEditPage
from selenium.common.exceptions import *

class Application(object):

    def __init__(self,driver,base_url):
        self.driver = driver
        self.login_page = LoginPage(driver,base_url)
        self.internal_page = InternalPage(driver,base_url)
        self.film_edit_page = FilmEditPage(driver,base_url)
        self.film_page = FilmPage(driver,base_url)
        self.base_url = base_url
        self.wait = WebDriverWait(driver,10)

    def add_film(self,film):
        self.internal_page.add_movie_button.click()
        self.film_edit_page.is_this_page
        self.film_edit_page.imdb_number_field.clear()
        self.film_edit_page.imdb_number_field.send_keys(film.imdbnumber)
        self.film_edit_page.title_field.clear()
        self.film_edit_page.title_field.send_keys(film.title)
        self.film_edit_page.year_field.clear()
        self.film_edit_page.year_field.send_keys(film.year)
        self.film_edit_page.save_button.click()

    def del_film(self):
        self.internal_page.film_present.click()
        self.film_page.is_this_page
        self.film_page.film_delete_button.click()
        alert = self.driver.switch_to_alert()
        alert.accept()

    def search_film(self,film):
        self.internal_page.search_field.clear()
        self.internal_page.search_field.send_keys(film.title)
        self.internal_page.search_field.send_keys(Keys.ENTER)

    def login(self, user):
        driver = self.driver
        self.login_page.username_field.clear()
        self.login_page.username_field.send_keys(user.username)
        self.login_page.password_field.clear()
        self.login_page.password_field.send_keys(user.password)
        self.login_page.submit_button.click()

    def logout(self):
        self.internal_page.logout_button.click()
        alert = self.driver.switch_to_alert()
        alert.accept()

    def go_to_home_page(self):
        self.driver.get(self.base_url)

    def is_page_film(self):
        try:
            self.wait.until(presence_of_element_located((By.LINK_TEXT,"Remove")))
            return True
        except WebDriverException:
            return False

    def is_logged_in(self):
        driver = self.driver
        try:
            self.wait.until(presence_of_element_located((By.CSS_SELECTOR,"nav")))
            return True
        except WebDriverException:
            return False

    def is_not_logged_in(self):
        driver = self.driver
        try:
            self.wait.until(presence_of_element_located((By.ID,"loginform")))
            return True
        except WebDriverException:
            return False

    def film_is_avaliable(self):
        try:
            self.wait.until(presence_of_element_located((By.CLASS_NAME,"movie_cover")))
            return True
        except WebDriverException:
            return False

    def film_is_not_avaliable(self):
        try:
            self.wait.until(invisibility_of_element_located((By.CLASS_NAME,"movie_cover")))
            return True
        except WebDriverException:
            return False